package com.zyh.service;

import com.zyh.dao.TaskMapper;
import com.zyh.dao.TrandMapper;
import com.zyh.pojo.FundPayPoJo;
import com.zyh.pojo.Zjywpt_yg_trand_infos;
import com.zyh.service.impl.FundPayServiceImpl;
import com.zyh.util.ConfigInfo;
import com.zyh.util.Dom23;
import com.zyh.util.RandomNumber;
import com.zyh.util.SocketUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shihan
 * @date 2020-12-01-19:12
 */
@Service
public class TrandServiceImpl implements TrandService {

    @Autowired
    private TaskMapper taskMapper;


    @Autowired
    private FundPayServiceImpl fundPayService;

    //调用dao层的操作，设置一个set接口，方便Spring管理
    private TrandMapper trandMapper;
    public void setTrandMapper(TrandMapper trandMapper) {
        this.trandMapper = trandMapper;
    }

    public int addTrand(Zjywpt_yg_trand_infos zjywpt_yg_trand_infos) {
        return trandMapper.addTrand(zjywpt_yg_trand_infos);
    }



    public int updateBTrand(Zjywpt_yg_trand_infos zjywpt_yg_trand_infos) {
        return trandMapper.updateTrand(zjywpt_yg_trand_infos);
    }

    public Zjywpt_yg_trand_infos queryBookById(String id) {
        return this.trandMapper.queryByBookID(id);
    }


    public void queryBigAccountResult() {

        //先去查询是否 正常以外的 记录，有的话进行再次查询

        FundPayPoJo fundPayPoJo = new FundPayPoJo();

        try {

            List<FundPayPoJo> lists = this.taskMapper.queryAllFund();

            if (lists.size() > 0) {

                //获得 数据库中‘大额交易的结果状态’
                for(FundPayPoJo fundPayPoJo1 : lists){

                    //拿到流水号，去ESB中进行查询
                    String  BsnSrlNo = fundPayPoJo1.getBsnSrlNo();

                     String   SrlSt =  this.interbankTramsferBigLog(BsnSrlNo);

                    ////网银返回码 交易中对应的码{000000 、}， 成功对应的码， 失败对应的码

                     //SrlSt = "3";
                    int times  = fundPayPoJo1.getTimes();

                    if("3".equals(SrlSt)){
                        //处理一次 times增加一次
                        fundPayPoJo1.setTimes(++times);
                        //从对象中取出来， 然后再设置进去
                        //添加次数控制 times
                        this.taskMapper.updateFund(fundPayPoJo1);

                        continue;
                    }
                    //交易成功
                    else if ("1".equals(SrlSt)){

                        //将数据库中的字段进行重新更新为 成功状态
                        fundPayPoJo1.setStatus("90");
                        fundPayPoJo1.setStatusMessage("大额查询--查询支付状态成功");

                        this.taskMapper.updateFund(fundPayPoJo1);
                    }else {
                        //交易失败

                        fundPayPoJo1.setTimes(++times);
                        this.taskMapper.updateFund(fundPayPoJo1);

                    }

                }

                System.out.println("hasNext is true"+lists.size());
            }


        }catch (Exception e){

            System.out.println(e.getLocalizedMessage()+e.getMessage());
        }
        //this.taskMapper.queryAllFund();



        // 如果ESB返回时成功的状态，那么久更新本地库记录
        //如果没有成功，继续下一个

    }

    //超网--跨行转账--大额第三方转账交易查询
    public String interbankTramsferBigLog(String CnlSrlNo) {

        Document doc = DocumentHelper.createDocument();
        Element service = doc.addElement("service");
        Element SysHead = service.addElement("SysHead");
        Element AppHead = service.addElement("AppHead");
        Element LocalHead = service.addElement("LocalHead");
        Element Body = service.addElement("Body");

        Element TxnTm = SysHead.addElement("TxnTm");
        Element BsnSrlNo = SysHead.addElement("BsnSrlNo");

        Element SvcCd = SysHead.addElement("SvcCd");
        Element ScnCd = SysHead.addElement("ScnCd");
        Element CnsmrSysNo = SysHead.addElement("CnsmrSysNo");
        Element TxnDt = SysHead.addElement("TxnDt");


        String uuid = RandomNumber.productBsnSrNo();
        BsnSrlNo.setText(uuid);// UUID.randomUUID().toString()

        SvcCd.setText("300630001");
        ScnCd.setText("01");
        CnsmrSysNo.setText(ConfigInfo.getConfigInfoByName("esb.CnsmrSysNo"));
        TxnDt.setText(RandomNumber.productTxnDt());
        TxnTm.setText(RandomNumber.productTxnTm());

        Element InstNo = AppHead.addElement("InstNo");
        Element OtltsNo = AppHead.addElement("OtltsNo");
        Element CnlCd = AppHead.addElement("CnlCd");
        Element UsrNo = AppHead.addElement("UsrNo");

        InstNo.setText("10001");
        OtltsNo.setText("10001");
        CnlCd.setText(ConfigInfo.getConfigInfoByName("esb.cnlCd"));
        UsrNo.setText("2010228");

        Element TxnTp = LocalHead.addElement("TxnTp");

        TxnTp.setText("ComTfrOut");//跨行转账交易固定


        Element CnlSrlNoEle = Body.addElement("CnlSrlNo");

        CnlSrlNoEle.setText(CnlSrlNo);


        //调用Socket发送 【返回ESB响应】
        String xmlESBResponse = SocketUtils.sendData(doc.asXML());

        System.out.println("esb-response" + xmlESBResponse);

        String srtls = "";

        try {
            srtls = Dom23.doESBResponseObjBigTaskLog(xmlESBResponse, uuid);


        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return srtls;
    }
}
